<template>
  <div class="quantity">
    <div :class="{ disabled: !isAdd }" @click="add">+</div>
    <input type="tel" v-model="count" />
    <div :class="{ disabled: !isLess }" @click="scss">-</div>
  </div>
</template>

<script>
export default {
  name: "quantity",
  props: {
    total: {
      type: Number,
      default: 1
    },
    num: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      count: 1,
      isAdd: true,
      isLess: false
    };
  },
  watch: {
    num(val) {
      this.count = val;
    }
  },
  methods: {
    add() {
      let count = parseInt(this.count);
      if (this.total < count + 1) {
        this.isAdd = false;
      } else {
        this.isLess = true;
        count++;
      }
      this.count = count;
    },
    less() {
      if (this.count - 1 < 1) {
        this.isLess = false;
      } else {
        this.isAdd = true;
        this.count--;
      }
    }
  },
  created() {}
};
</script>

<style scoped  lang="scss" scoped>
.quantity {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  div {
    width: 32px;
    height: 32px;
    background-color: #dbdbdb;
    font-size: 14px;
    color: #333;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
  }
  div.disabled {
    background-color: #f3f3f3;
    color: #cbcbcb;
  }
  input {
    font-size: 16px;
    color: #333;
    font-weight: 600;
    display: block;
    height: 32px;
    box-sizing: border-box;
    border: solid #dbdbdb 1px;
    width: 80px;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
